var server = {
	funcs: {
		login:        'LOGIN',
		logout:       'LOGOUT',
		isLogged:     'ISLOGGED',
		getBasicData: 'GETBASICDATA',
		getTableData: 'GETTABLEDATA'
	},

	getURL:function(func,params)
	{
		var _ret = config.URL_EXEC + '?Function='+func;
		if('undefined' != typeof params && params)
		{
			for(var param in params)
			{
				_ret += '&'+param+'='+encodeURIComponent(params[param]);
			}
		}
		return _ret;
	},

	request:function(func,params,callback)
	{
		if('function' != typeof params)
		{
			_params = params;
		}
		else
		{
			callback = params;
			_params = false;
		}
		$.getJSON(server.getURL(func,_params),callback);
	},

	requests: {
		login:function()
		{
			var Hostname = $('#loginHostname').val();
			var Username = $('#loginUsername').val();
			var Password = $('#loginPassword').val();
	
			if(Hostname == '' || Username == '')
			{
				$('#loginBoxStatus').html('<i><span class="colorRed">Please fill in the Hostname and the Username</span></i>');
				return;
			}
	
			$('#loginBoxStatus').html('<img src="images/loader.gif" align="top" border="0"> <i>Working...</i>');
			$('#loginBoxButton').attr({disabled:true});
			$('#loginHostname').attr({disabled:true});
			$('#loginUsername').attr({disabled:true});
			$('#loginPassword').attr({disabled:true});
			server.request(
				server.funcs.login,
				{
					Hostname:Hostname,
					Username:Username,
					Password:Password
				},
				function(data)
				{
					if(data.Logged)
					{
						LOGGED = true;
						MYSQL_HOST = Hostname;
						MYSQL_USER = Username;
						MYSQL_PASS = Password;
						boxes.login.hide(basic.start);
					}
					else
					{
						$('#loginBoxStatus').html('<i><span class="colorRed">Could not login to mySQL server</span></i>');
						$('#loginBoxButton').attr({disabled:false});
						$('#loginHostname').attr({disabled:false});
						$('#loginUsername').attr({disabled:false});
						$('#loginPassword').attr({disabled:false});
					}
				}
			);
		},

		logout:function()
		{
			server.request(
				server.funcs.logout,
				function(data)
				{
					if(!data.Logged)
					{
						LOGGED = false;
						$('#mainContainer').fadeOut('slow',function(){
							boxes.login.show();
						});
					}
				}
			);
		},

		getBasicData:function()
		{
			server.request(
				server.funcs.getBasicData,
				function(data)
				{
					if(data.DatabaseList)
					{
						menus.left.fill(data.DatabaseList,data.TableList);
					}
					basic.resizeMainContent();
				}
			);
		},

		getTableData:function(db,tb)
		{
			menus.left.hide();
			basic.setContentWait();
			server.request(
				server.funcs.getTableData,
				{
					Database:db,
					Table:tb
				},
				function(data)
				{
					basic.clearContent();
					if(data.Error)
					{
						boxes.error.show(data.Error,function(){menus.left.show();});
						return;
					}
					menus.tableData.draw();
					tableData.draw(data.Fields,data.Data);
				}
			);
		}
	}
};